// Package dsweb 提供Web服务的封装和工具函数
package dsweb

import (
	"fmt"
	"gitee.com/deep-sea-tech/ds-go-client/dsregistry"
	"gitee.com/deep-sea-tech/ds-go-client/tools"
	"github.com/gin-gonic/gin"
)

type Application struct {
	Engine    *gin.Engine
	Config    *Config
	pathArray []string
}

func NewApplication(opts *Config) *Application {
	err := dsregistry.RegistryCenterInstance.Register(&dsregistry.ServiceInstance{
		ServiceMeta: &dsregistry.ServiceMeta{
			ID:         opts.Name,
			Name:       opts.Name,
			Address:    tools.GetLocalIP(),
			Port:       opts.Port,
			Protocol:   "http",
			Weight:     0,
			Tags:       nil,
			Metadata:   nil,
			Status:     "",
			RegisterAt: 0,
			LastBeat:   0,
		},
		TTL: 0,
	})
	if err != nil {
		panic("err:" + err.Error())
	}
	return &Application{
		Engine: gin.Default(),
		Config: opts,
	}
}

func (app *Application) Router(router func(engine *gin.Engine)) {
	router(app.Engine)
}

func (app *Application) Start() {

	err := app.Engine.Run(fmt.Sprintf(":%d", app.Config.Port))
	if err != nil {
		panic(err.Error())
	}
}
